package cn.helper;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import com.unity3d.player.UnityPlayerGameActivity;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends UnityPlayerGameActivity {
    public interface ActivityResult {
        void onActivityResult(int requestCode, int resultCode, Intent data);
    }
    public static List<ActivityResult> activityResultCallbacks = new ArrayList<>();
    private final static String TAG = "[Unity]";
    private String scheme = "";
    private String schemeHost = "";
    private String schemeParams = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "MainActivity.onCreate()");

        //接收SchemeURL
        Intent intent = getIntent();
        Uri uri = intent.getData();
        if (uri != null) {
            this.scheme = uri.getScheme();
            this.schemeHost = uri.getHost();
            this.schemeParams = uri.getQuery();
        }
        Log.i(TAG, "scheme: " + this.scheme);
        Log.i(TAG, "schemeHost: " + this.schemeHost);
        Log.i(TAG, "schemeParams: " + this.schemeParams);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (activityResultCallbacks != null) {
            for (int i = 0; i < activityResultCallbacks.size(); i++) {
                ActivityResult listener = activityResultCallbacks.get(i);
                if (listener != null) {
                    listener.onActivityResult(requestCode, resultCode, data);
                }
            }
        }
    }

    public String getScheme() {
        return this.scheme;
    }

    public String getSchemeHost() {
        return this.schemeHost;
    }

    public String getSchemeParams() {
        return this.schemeParams;
    }
}